package com.example.spinning.service;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spinning.bean.*;
import com.example.spinning.mapper.WilleyMapper;
import com.example.spinning.utils.CraftManagementTimeUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author bayernxmt
 * @create 2020-11-11-11:15
 */
@Service
@Transactional
public class WilleyService {
    @Autowired
    private WilleyMapper willeyMapper;

    //查询所有 并且分页展示 : current表示当前页，size表示一页展示多少条数据
    public PaginationResult<Willey> selectAll(int current, int size){
        Page<Willey> pages = new Page<>(current,size);
        willeyMapper.selectPage(pages, null);
        List<Willey> list = pages.getRecords();
        int total = willeyMapper.selectCount(null);

        PaginationResult<Willey> pr = new PaginationResult<>();
        pr.setTotal(total);
        pr.setList(list);
        return pr;
    }

    //单、多条件查询 并且实现分页
    public PaginationResult<Willey> selectByMulParams(int current, int size, Willey willey, String dateTime){

        QueryWrapper<Willey> wrapper = new QueryWrapper();
        if (null!=willey.getBatch_no())
            wrapper.eq("batch_no", willey.getBatch_no());
        if (null!=willey.getName())
            wrapper.eq("name", willey.getName());
        if (null!=dateTime)
            wrapper.like("date", dateTime);

        Page<Willey> pages = new Page<>(current,size);
        willeyMapper.selectPage(pages, wrapper);
        int total = willeyMapper.selectCount(wrapper);
        List<Willey> list = pages.getRecords();

        PaginationResult<Willey> pr = new PaginationResult<>();
        pr.setTotal(total);
        pr.setList(list);
        return pr;
    }

    //根据id查询
    public Willey SelectById(int id){
        return willeyMapper.selectById(id);
    }

    //插入数据
    public int insert(Willey willey) {
        willey.setDate(CraftManagementTimeUtils.getTimestamp());
        int result = willeyMapper.insert(willey); //result为受到影响的条数
        return result;
    }


    //批量删除数据（与删除一条合并）
    public int delete(List<Integer> ids){
        int result = willeyMapper.deleteBatchIds(ids);
        return result;
    }

    //修改数据(已经实现了动态sql)
    public int update(Willey willey){
        willey.setDate(new Timestamp(new Date().getTime()));
        int result = willeyMapper.updateById(willey);
        return result;
    }

    //内外表一对多查询时，封装数据
    public List<WilleyVo> getWilleyVoList(List<Willey> willeyList){
        List<WilleyVo> willeyVoList = new ArrayList<>();

        for (Willey element:willeyList) {
            WilleyVo willeyVo = new WilleyVo();
            willeyVo.setId(element.getId());
            willeyVo.setBatch_no(element.getBatch_no());
            willeyVo.setName(element.getName());
            willeyVo.setWorkshop(element.getWorkshop());
            willeyVo.setDate(element.getDate());
            willeyVo.setMachine_type(element.getMachine_type());
            willeyVo.setNum(element.getNum());
            willeyVo.setCylinder_speed(element.getCylinder_speed());
            willeyVo.setGauge_length(element.getGauge_length());
            willeyVo.setColor1(element.getColor1());
            willeyVo.setWool_oil(element.getWool_oil());
            willeyVo.setEnhancer(element.getEnhancer());
            willeyVo.setAntistat(element.getAntistat());
            willeyVo.setPercent2(element.getPercent2());

            willeyVoList.add(willeyVo);
        }
        return willeyVoList;
    }
}
